/*******************************************************************************
 * Package: com.hngtrust.iir.base.util
 * Type:    TreeUtils
 * Date:    2021/9/15 16:05
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.base.util;

import com.hngtrust.iir.base.dto.TreeDate;

import java.util.ArrayList;
import java.util.List;

/**
 * 树形转换工具类
 *
 * @author Yujiaqi
 * @date 2021/9/15 16:05
 */
public class TreeUtils {
    
    /**
     * 使用递归方法建树
     * @param
     * @return
     */
    public static List<TreeDate> buildByRecursive(List<TreeDate> testEntities) {
        List<TreeDate> trees = new ArrayList<>();
        for (TreeDate testEntity : testEntities) {
            if (testEntity.getParentId() == 0) {
                trees.add(findChildren(testEntity,testEntities));
            }
        }
        return trees;
    }
    
    /**
     * 递归查找子节点
     * @param
     * @return
     */
    public static TreeDate findChildren(TreeDate testEntity,List<TreeDate> testEntitys) {
        for (TreeDate projectBasicInfoDTO2 : testEntitys) {
            if(testEntity.getId().equals(projectBasicInfoDTO2.getParentId())) {
                if(testEntity.getChildren() == null) {
                    testEntity.setChildren(new ArrayList<>());
                }
                //是否还有子节点，如果有的话继续往下遍历，如果没有则直接返回
                testEntity.getChildren().add(findChildren(projectBasicInfoDTO2,testEntitys));
            }
        }
        return testEntity;
    }
}
